#include "LEDtube.h"
#include <REGF51RC.H>

TubeDataBuff tubeData = {0};

code uint8_t tubeTansform[16] = {
	0x28, // 0
	0xEB, // 1
	0x32, // 2
	0xA2, // 3
	0xE1, // 4
	0xA4, // 5
	0x24, // 6
	0xEA, // 7
	0x20, // 8
	0xA0, // 9
	0x60, // A
	0x25, // b
	0x3C, // C
	0x23, // d
	0x34, // E
	0x74	// F
};

// 数码管显示函数
void tubeShow(uint8_t sw, uint8_t num)
{
	switch (sw)
	{
		case 0:
			tube1on;
			break;
		case 1:
			tube2on;
			break;
		case 2:
			tube3on;
			break;
		case 3:
			tube4on;
			break;
		default:
			tubeOff;
	}
	tubePin = tubeTansform[num];
}

void autoTubeShow()
{
	static uint8_t count = 0;
	count %= tubeNum;
	switch (count)
	{
		case 0:
			tubeShow(count++, tubeData.d0);
			break;
		case 1:
			tubeShow(count++, tubeData.d1);
			break;
		case 2:
			tubeShow(count++, tubeData.d2);
			break;
		case 3:
			tubeShow(count++, tubeData.d3);
			break;
	}
}

/**
 * @brief 数码管显示十进制数，最多9999
 *
 * @param num 需要显示的数
 */
void tubeDataSet_int(uint16_t num)
{
	tubeData.d3 = num % 10;
	num /= 10;
	tubeData.d2 = num % 10;
	num /= 10;
	tubeData.d1 = num % 10;
	num /= 10;
	tubeData.d0 = num % 10;
	num /= 10;
}

/**
 * @brief 数码管显示时间，输入为秒，自动转换为分：秒的结构，最多99：59
 *
 * @param time 计时的秒数
 */
void tubeDataSet_time(uint16_t time)
{
	uint8_t secend = time % 60;
	time /= 60;
	tubeData.d3 = secend % 10;
	tubeData.d2 = secend / 10;
	tubeData.d1 = time % 10;
	tubeData.d0 = time / 10;
}
